<script setup lang="ts">
import { getKnowledgePage } from '@/services/consult'
import KnowledgeCard from './KnowledgeCard.vue'
import type {
  KnowledgeType,
  KnowledgeParams,
  KnowledgeList
} from '@/types/consult'
import { ref } from 'vue'

const props = defineProps<{
  type: KnowledgeType
}>()
console.log(props.type)
// 发请求获取文章列表数据

const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 10
})

const loading = ref(false)
const finished = ref(false)
const list = ref<KnowledgeList>([])

// getKnowledgePage(params.value).then((res) => {
//   // console.log(res)
//   list.value = res.data.rows
// })

const getList = async () => {
  // 自动将loading改成true
  const res = await getKnowledgePage(params.value)
  list.value.push(...res.data.rows) // 追加数据
  // 获取数据后手动将loading改成false
  loading.value = false
  params.value.current++ // 下一次触底获取下一页的数据

  if (list.value.length === res.data.total) {
    // 没有更多数据了
    finished.value = true
  }
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      @load="getList"
      :finished="finished"
      finished-text="我是有底线的"
    >
      <knowledge-card
        v-for="item in list"
        :key="item.id"
        :item="item"
      ></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
